package com.taobao.jingling.web.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.taobao.api.TaobaoApiException;
import com.taobao.api.TaobaoJsonRestClient;
import com.taobao.api.TaobaoRestClient;
import com.taobao.api.model.TradesGetResponse;
import com.taobao.api.model.TradesSoldGetRequest;

public class TradesSales extends HttpServlet {
	private static final transient Log log = LogFactory.getLog(GetUserProfile.class);

	/**
	 * Constructor of the object.
	 */
	public TradesSales() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		 this.doPost(request, response);
	}

	public Date stringConvertDate(String str){
		try {
		  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return sdf.parse(str);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		     String appkey = "28097";
		     String secret = "42ff2240438d11deae718c0bea828c3d";
		     
		     try {
				TaobaoRestClient client= new TaobaoJsonRestClient(appkey,secret);
				
				 TradesSoldGetRequest tsReq=new TradesSoldGetRequest();
				 tsReq.setFields("seller_nick,buyer_nick,iid,title,price,pic_path,num,created,type,alipay_no,payment,status");
				 tsReq.setStartCreated(this.stringConvertDate("2008-12-01 01:01:01"));
				 tsReq.setEndCreated(this.stringConvertDate("2009-05-01 01:01:01"));
				 tsReq.setPageSize(new Integer(100));
				 tsReq.setPageNo(new Integer(1));
				 TradesGetResponse tgRes=client.tradesSoldGet(tsReq,appkey);
				 
				 log.info(tgRes.getBody());
				 
				 if(!tgRes.isSuccess())	
					{
						if(tgRes.isRedirect()){
							request.setAttribute("url", tgRes.getRedirectUrl(URLEncoder.encode(request.getRequestURL().append('?').append("appkey=").append(appkey).append("&secret=").append(secret).toString(),"utf-8")));
						}else{
							request.setAttribute("msg", tgRes.getMsg());
						}
					}else{
						request.setAttribute("trades", tgRes.getTrades());
					}
				} catch (TaobaoApiException e) {
					throw new ServletException(e);
				}
				request.getRequestDispatcher("/getTradeSales.jsp").forward(request, response);
				 
	
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
